import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Admin from '@/pages/Admin'
import DataShow from '@/pages/DataShow'
import Delivery from '@/pages/Delivery'
import Order from '@/pages/Order'
import Suggestion from '@/pages/Suggestion'
import User from '@/pages/User'
import Goods from '@/pages/Goods'

Vue.use(Router)
const routes = [{
    path: '/',
    component: Order,
  },
  {
    path: '/user',
    component: User,
    meta: {
      requireAuth: true,
    }
  },
  {
    path: '/order',
    component: Order,
    meta: {
      requireAuth: true,
    }
  },
  {
    path: '/delivery',
    component: Delivery,
    meta: {
      requireAuth: true,
    }
  },
  {
    path: '/Suggestion',
    component: Suggestion,
    meta: {
      requireAuth: true,
    }
  },
  {
    path: '/admin',
    component: Admin,
    meta: {
      requireAuth: true,
    }
  },
  {
    path: '/goods',
    component: Goods,
    meta: {
      requireAuth: true,
    }
  }

];
const router = new Router({
  mode: 'history',
  base: '/m',
  scrollBehavior(to, from, savedPosition) {
    //判断如果滚动条的位置存在直接返回到当前位置，否者返回到起点
    return savedPosition || {
      x: 0,
      y: 0
    }
  },
  routes
});



// 路由拦截器
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requireAuth)) {
    let isValid = sessionStorage.getItem('isValid');
    if (isValid == 'true') {
      next();
    } else {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        }
      })
    }
  } else {
    next();
  }
})
export default router;
